package com.ruoyi.bigScreen.service.impl;

import com.ruoyi.bigScreen.mapper.BigScreenAnnualTaskProgressMapper;
import com.ruoyi.bigScreen.service.IBigScreenAnnualTaskProgressService;
import com.ruoyi.common.core.domain.business.waterDistribution.AnnualTaskProgress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 水利服务站站年度任务进度Service业务层处理
 *
 * @author hwd
 * @date 2023-07-14
 */
@Service
public class BigScreenAnnualTaskProgressServiceImpl implements IBigScreenAnnualTaskProgressService
{
    @Autowired
    private BigScreenAnnualTaskProgressMapper annualTaskProgressMapper;

    /**
     * 查询水利服务站站年度任务进度
     *
     * @param id 水利服务站站年度任务进度主键
     * @return 水利服务站站年度任务进度
     */
    @Override
    public AnnualTaskProgress selectAnnualTaskProgressById(Long id)
    {
        return annualTaskProgressMapper.selectAnnualTaskProgressById(id);
    }

    /**
     * 查询水利服务站站年度任务进度列表
     *
     * @param annualTaskProgress 水利服务站站年度任务进度
     * @return 水利服务站站年度任务进度
     */
    @Override
    public List<AnnualTaskProgress> selectAnnualTaskProgressList(AnnualTaskProgress annualTaskProgress)
    {
        List<AnnualTaskProgress> taskProgressList=  annualTaskProgressMapper.selectAnnualTaskProgressList(annualTaskProgress);
        if (taskProgressList.size()>0){
            for (AnnualTaskProgress a:taskProgressList
                 ) {
                a.setRate(a.getTotalWaterFees().divide(a.getTaskAmount(),4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")));
            }
            taskProgressList=taskProgressList.stream().sorted(Comparator.comparing(AnnualTaskProgress::getRate).reversed())
                    .collect(Collectors.toList());
        }

        return taskProgressList;
    }

    /**
     * 新增水利服务站站年度任务进度
     *
     * @param annualTaskProgress 水利服务站站年度任务进度
     * @return 结果
     */
    @Override
    public int insertAnnualTaskProgress(AnnualTaskProgress annualTaskProgress)
    {
        return annualTaskProgressMapper.insertAnnualTaskProgress(annualTaskProgress);
    }

    /**
     * 修改水利服务站站年度任务进度
     *
     * @param annualTaskProgress 水利服务站站年度任务进度
     * @return 结果
     */
    @Override
    public int updateAnnualTaskProgress(AnnualTaskProgress annualTaskProgress)
    {
        return annualTaskProgressMapper.updateAnnualTaskProgress(annualTaskProgress);
    }

    /**
     * 批量删除水利服务站站年度任务进度
     *
     * @param ids 需要删除的水利服务站站年度任务进度主键
     * @return 结果
     */
    @Override
    public int deleteAnnualTaskProgressByIds(Long[] ids)
    {
        return annualTaskProgressMapper.deleteAnnualTaskProgressByIds(ids);
    }

    /**
     * 删除水利服务站站年度任务进度信息
     *
     * @param id 水利服务站站年度任务进度主键
     * @return 结果
     */
    @Override
    public int deleteAnnualTaskProgressById(Long id)
    {
        return annualTaskProgressMapper.deleteAnnualTaskProgressById(id);
    }
}
